package com.dao.userdao;

import com.mapper.UserMapper;
import com.pojo.Author;
import com.pojo.FansItem;
import com.pojo.FollowItem;
import com.pojo.User;
import com.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

/**
 * @author 师锦涛
 * @create 2022/8/12
 * @description 个人中心的数据库操作
 */
public class PersonalCenter {
      /*
        获取用户基本信息
       */
      public static User get_user_info(User user) throws IOException {
            System.out.println("[info]从数据库取出个人信息");
            //获取sqlSession对象
            SqlSession sqlSession= MybatisUtil.getSqlSession();
            //3.获取Mapper接口的代理对象
            UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
            //4.执行方法
            User user1=userMapper.get_user_info(user);
            //提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();
            //返回实体类
            return user1;
      }

      /**
       *获取用户成就
       */
      public static User get_user_success(String email)throws IOException{
            System.out.println("[info]从数据库取出用户成就");
            //获取sqlSession对象
            SqlSession sqlSession= MybatisUtil.getSqlSession();
            //3.获取Mapper接口的代理对象
            UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
            //4.执行方法
            User user1=userMapper.get_user_success(email);
            //提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();
            //返回实体类
            return user1;
      }
      /*
        更新用户信息
       */
      public static void update_user_info(User user)throws IOException{
            System.out.println("[info]更新数据库中的个人信息");
            //获取sqlSession对象
            SqlSession sqlSession= MybatisUtil.getSqlSession();
            //3.获取Mapper接口的代理对象
            UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
            //4.执行方法
           userMapper.update_user_info(user);
            //提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();
      }
      /**
       * 获取关注列表
       */
      public static List<FollowItem> get_following_list(String userid)throws IOException{
            System.out.println("[info]获取关注列表");
            //获取sqlSession对象
            SqlSession sqlSession= MybatisUtil.getSqlSession();
            //3.获取Mapper接口的代理对象
            UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
            //4.执行方法
            List<FollowItem> followlist;
            followlist=userMapper.get_following_list(userid);
            //提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();
            return followlist;
      }
      /**
       * 取消关注
       */
      public static void cancel_concern(String userid,String following_id)throws IOException{
            System.out.println("[info]取消关注");
            //获取sqlSession对象
            SqlSession sqlSession= MybatisUtil.getSqlSession();
            //3.获取Mapper接口的代理对象
            UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
            //4.执行方法
            userMapper.cancel_concern(userid,following_id);
            //提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();
      }

      /**
       *用户关注
       */
      public static void concern(String userid,String following_id)throws IOException{
            System.out.println("[info]用户关注");
            //获取sqlSession对象
            SqlSession sqlSession= MybatisUtil.getSqlSession();
            //3.获取Mapper接口的代理对象
            UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
            //4.执行方法
            userMapper.user_concern(userid,following_id);
            //提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();
      }

      /**
       * 获取粉丝列表
       */
      public static List<FansItem> get_fans_list(String userid)throws IOException{
            System.out.println("[info]获取粉丝列表");
            //获取sqlSession对象
            SqlSession sqlSession= MybatisUtil.getSqlSession();
            //3.获取Mapper接口的代理对象
            UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
            //4.执行方法
            List<FansItem>fansItems;
            fansItems=userMapper.get_fans_list(userid);
            //提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();
            return fansItems;
      }

      /**
       * 注销账号
       */
        public static void account_logout(String userid)throws IOException{
              //获取sqlSession对象
              SqlSession sqlSession= MybatisUtil.getSqlSession();
              //3.获取Mapper接口的代理对象
              UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
              //4.执行方法
              userMapper.logout_account(userid);
              userMapper.del_concern(userid);
              //提交事务
              sqlSession.commit();
              //5.释放资源
              sqlSession.close();
        }

      /**
       * 判断旧密码
       */
        public static boolean judge_pwd(String pwd,String email)throws IOException{
              boolean resp=true;
              //获取sqlSession对象
              SqlSession sqlSession= MybatisUtil.getSqlSession();
              //3.获取Mapper接口的代理对象
              UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
              //4.执行方法
              User user1=userMapper.judge_pwd(pwd,email);
              //提交事务
              sqlSession.commit();
              //5.释放资源
              sqlSession.close();
              if(user1==null){
                    resp=false;
              }
              return resp;
        }

      /**
       * 获取作者信息
       */
      public static Author get_author_info(String author_id,String self_id)throws IOException{
            System.out.println("[info]从数据库取出用户成就");
            //获取sqlSession对象
            SqlSession sqlSession= MybatisUtil.getSqlSession();
            //3.获取Mapper接口的代理对象
            UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
            //4.执行方法
            Author author=userMapper.get_author_info(author_id,self_id);
            //提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();
            //返回实体类
            return author;
      }

      /**
       * 获取用户总数
       */
      public static int get_user_count()throws IOException{
            System.out.println("[info]获取用户总数");
            //获取sqlSession对象
            SqlSession sqlSession= MybatisUtil.getSqlSession();
            //3.获取Mapper接口的代理对象
            UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
            //4.执行方法
            int totalCount=userMapper.get_user_count();
            //提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();

            return totalCount;
      }

      /**
       * 获取所有用户
       */
      public static List<User> get_all_user(int start)throws IOException{
            System.out.println("[info]获取所有用户");
            //获取sqlSession对象
            SqlSession sqlSession= MybatisUtil.getSqlSession();
            //3.获取Mapper接口的代理对象
            UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
            //4.执行方法
            List<User> users=userMapper.get_all_user(start);
            //提交事务
            sqlSession.commit();
            //5.释放资源
            sqlSession.close();

            return users;
      }

}
